package com.hzit.seckill.controller;

import com.hzit.common.utils.MyDate;
import com.hzit.seckill.entity.TbSeckillGoods;
import com.hzit.seckill.service.SeckillOrderService;
import com.hzit.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Controller
public class SeckillController {
	@Autowired
	private SeckillService seckillService;
	@Autowired
	private SeckillOrderService orderService;

	//1. 秒杀首页
	@GetMapping({"/","seckill-index.html"})
	public String seckillIndex(Model model){
		//1.1 将数据加载到redis中
		seckillService.loadData();
		//1.2 从redis中取数据
		List<TbSeckillGoods> seckillGoodsList = seckillService.getDataFromRedis();
		model.addAttribute("seckillGoodsList",seckillGoodsList);
		//1.3 将5个时间段放到model中
		List<Date> dateMenus = MyDate.getDateMenus();
		model.addAttribute("dateMenus",dateMenus);

		return "seckill-index";
	}

	//2. 根据商品id查询商品
	@GetMapping("/seckill/item/{id}")
	public String findItem(@PathVariable("id") String id,Model model){
		TbSeckillGoods goods = seckillService.findById(id);
		model.addAttribute("goods",goods);
		return "seckill-item";
	}

	//3. 下单操作
	@GetMapping("/seckill/saveOrder/{id}")
	public void saveOrder(@PathVariable("id") String id){

		orderService.saveOrder(id);
	}
}
